package com.asset.inventory.base

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Window
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.asset.inventory.kt.toast
import java.util.zip.Inflater

/**
 *    @author : sibb
 *    date   : 21-3-16
 *    desc   : activity 基类
 */
open abstract class BaseActivity<VM: BaseViewModel, V: ViewDataBinding>: AppCompatActivity() {

    open lateinit var mViewModel: VM
    open lateinit var binding: V

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 去掉窗口标题
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏顶部的状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        binding = DataBindingUtil.setContentView<V>(this,getLayoutId())
        mViewModel = ViewModelProvider.AndroidViewModelFactory(BaseApp.instance).create(initViewModel())
        val viewModelId = initVariableId()
        binding.lifecycleOwner = this
        binding.setVariable(viewModelId,mViewModel)
        lifecycle.addObserver(mViewModel)
        initView()
        initListener()
    }

    open fun initListener() {
        mViewModel.toastStr.observe(this, Observer {
            toast(it)
        })

        mViewModel.toastResId.observe(this, Observer {
            toast(it)
        })
    }

    abstract fun getLayoutId(): Int

    abstract fun initVariableId(): Int

    abstract fun initViewModel():  Class<VM>

    abstract fun initView()
}